<template>
  <div class="login">
    <div class="box">
      <div class="head">
        <img src="https://s.weituibao.com/1582958061265/mlogo.png" alt="" />
        <div class="right">
          <h2>新蜂商城</h2>
          <p>Vue3.0 后台管理系统</p>
        </div>
      </div>
      <div class="main">
        <el-form
          label-position="top"
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
          :size="formSize"
          status-icon
        >
          <el-form-item label="账号" prop="userName">
            <el-input v-model="ruleForm.userName" />
          </el-form-item>
          <el-form-item label="密码" prop="passwordMd5">
            <el-input show-password v-model="ruleForm.passwordMd5" />
          </el-form-item>
          <el-form-item>
            <p>登录表示您已同意<span style="color: #50b4ff">《服务条款》</span></p>
            <el-button
              type="primary"
              @click="submitForm(ruleFormRef)"
              style="width: 100%; height: 45px; "
              >立即登录</el-button
            >
          </el-form-item>
        </el-form>
        <el-checkbox v-model="checked" label="下次自动登录" size="large" />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import {api} from "../api/api"
import { useStore } from "../store/counter";
import type { FormInstance, FormRules } from "element-plus";
import {Md5} from "ts-md5";
import { ElMessage } from "element-plus";
import "element-plus/es/components/message/style/css"
const store = useStore();
const route = useRoute();
const router = useRouter();
const checked = ref(true);
interface RuleForm {
  userName: string;
  passwordMd5: string;
}

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  userName: "admin",
  passwordMd5: "",
});

const rules = reactive<FormRules<RuleForm>>({
  userName: [{ required: true, message: "请输入账号", trigger: "blur" }],
  passwordMd5: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
        const md5:any = new Md5
        md5.appendAsciiStr(ruleForm.passwordMd5)
        const passwordMd5 = md5.end()
        api.loginApi({userName:ruleForm.userName,passwordMd5}).then((res:any)=>{
        if(res.resultCode==200){
            localStorage.setItem("token",res.data);
            ElMessage.success("登录成功")
            router.push("/introduce")
        }else{
          ElMessage.error(res.message)
        }
     })
     
     
    } else {
      console.log("error submit!", fields);
    }
  });
};

</script>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  .box {
    width: 500px;
    // height: 500px;
    padding: 10px;
    background: #fff;
    box-shadow: 0 21px 41px 0 rgba(0, 0, 0, 0.2);
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 320px;
      margin: 20px auto;
      img {
        width: 150px;
        height: 150px;
      }
      .right {
        h2 {
          color: #1baeae;
          font-size: 39px;
        }
      }
    }
    .main {
      width: 80%;
      margin: auto;
    }
  }
}
</style>
